package org.pplb5.siresto;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.AdapterView.OnItemClickListener;

public class DaftarMejaPersonal extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		if (android.os.Build.VERSION.SDK_INT > 9) {
		    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
		    StrictMode.setThreadPolicy(policy);
		}
		
		setContentView(R.layout.activity_daftar_meja_personal);
		GridView list = (GridView) findViewById(R.id.mejaPersonal);
		/*String[] meja = { "Meja A", "Meja B", "Meja C",
			"Meja D", "Meja E", "Meja F", "Meja G", "Meja H",
				"Meja I", "Meja J" };
		ArrayAdapter<String> adapt = new ArrayAdapter<String>(this,
				R.layout.meja_view, meja);
		list.setAdapter(adapt); */
		
		Intent i = getIntent();
        final String namaResto = i.getStringExtra("NAMA_RESTO");
		
		ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
		postParameters.add(new BasicNameValuePair("namaRestoran",
				namaResto.toString()));
		
		String response = null;

		try {
			response = CustomHttpClient.executeHttpPost(
					"http://annisadwinidia.hostei.com/getMeja.php", postParameters);

			String result = response.toString();  

			try{
				JSONArray jArray = new JSONArray(result);
	    		int n = jArray.length();
	    		String[] meja = new String[n];
	    		
	    		for(int ii = 0; ii<n; ii++){
	    			JSONObject json = jArray.getJSONObject(ii);
	    			 meja[ii] = "Meja Nomor " + json.getString("noMeja") + "\n" + "Kapasitas: " + json.getString("kapasitas") + " orang";	
	    		}
	    		ArrayAdapter<String> adapt = new ArrayAdapter<String>(this,
	    				R.layout.meja_view, meja);
	    		list.setAdapter(adapt);
	    		
			}
			catch(JSONException e){
				Log.e("log_tag", "Error parsing data "+e.toString());
			}

		}
		catch (Exception e) {
			Log.e("log_tag","Error in http connection!!" + e.toString());     
		}
		
		
		list.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent meja = new Intent(getApplicationContext(),FormReservasi.class);
				//daftarMenu.putExtra(", namaResto);
				startActivity(meja);
				
			}
			
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.daftar_meja_personal, menu);
		return true;
	}

}
